package com.ftg.learn.chapter02.java.pool;

import java.util.HashMap;
import java.util.Map;

/**
 * 学习容器 -
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/12/25 上午8:27
 */
public class Second {

    public static  void main(String[] args) {
        //动物  集合  容器  池 池化
        Map<String,Object> containers = new HashMap<>(4);

        // 生产者
        //给我写一个方法 createInstance();
        Second f = new Second();
        Dog dog1 = f.createInstance3(Dog.class);

        dog1.setName("小花");
        dog1.setColor("黑");
        dog1.setType("quan");

        System.out.println(dog1);

        containers.put("dog",f.animalsFactory(Dog.class,"小花","quan","黑"));
        containers.put("bird",f.animalsFactory(Bird.class,"小花","quan","黑"));


        //调用 消费者
        containers.get("dog");


    }

    /**
     * 第四种
     */
    public <T> T createInstance3(Class<T> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 第一种
     * @param clazz
     * @param name
     * @param type
     * @param color
     * @return
     * @param <T>
     */
    public <T extends Animals> T animalsFactory(Class<T> clazz,String name,String type,String color) {
        T animals =  createInstance3(clazz);
        animals.setName(name);
        animals.setType(type);
        animals.setColor(color);
        return animals;
    }

}
